package com.cykj.pos.controller.api;

import com.ayg.openapi.OpenApiClient;
import com.cykj.common.config.RuoYiConfig;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.core.domain.entity.SysUser;
import com.cykj.pos.domain.BizCashoutFee;
import com.cykj.pos.domain.WithdrawalRateTemplate;
import com.cykj.pos.domain.dto.CashOutDTO;
import com.cykj.pos.domain.dto.CashOutVO;
import com.cykj.pos.mapper.BizMicroInfoMapper;
import com.cykj.pos.mapper.BizWithdrawalRecordsMapper;
import com.cykj.pos.service.*;
import com.cykj.pos.util.LoginUserUtils;
import com.cykj.system.service.ISysDictDataService;
import com.cykj.system.service.ISysUserService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 支付控制器
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/pos/api/v2/payment")
public class PosV2AppPaymentController  implements EnvironmentAware {

    private final IBizWalletService walletService;

    private final ISysUserService sysUserService;

    private final IBizVerifyCodeService verifyCodeService;

    private final IBizMicroInfoService microInfoService;

    private final IBizMerchantService merchantService;

    private final ISysDictDataService sysDictDataService;

    private final IBizMerchBillService merchBillService;

    private final IBizCashoutFeeService cashoutFeeService;

    private final RuoYiConfig config;

    public static OpenApiClient client;

    public static String privateKey;

    public static String appId;


    @Resource
    private BizWithdrawalRecordsMapper bizWithdrawalRecordsMapper;
    @Resource
    private BizMicroInfoMapper bizMicroInfoMapper;

    @ApiOperation(value = "我的钱包-去提现")
    @PostMapping("/toCashOut")
    public AjaxResult toCashOut(@RequestBody CashOutDTO cashOutDTO) {
        AjaxResult ajaxResult = AjaxResult.success("我的钱包-去体现数据获取成功");
        // 获取用户信息
        Long userId = LoginUserUtils.getLoginUserId();
        // 获取用户信息
        SysUser sysUser = sysUserService.selectUserById(userId);
        cashOutDTO.setUserId(userId);
        CashOutVO cashOutVO = microInfoService.getMerchMicroInfoByUserIdAndAccountType(cashOutDTO);
        BizCashoutFee cashoutFee = cashoutFeeService.getCashoutFeeByType(cashOutDTO);
        cashoutFee.setMinAmount(new BigDecimal("10"));
        ajaxResult.put("data", cashOutVO);
        ajaxResult.put("minAmount", cashoutFee.getMinAmount());
        ajaxResult.put("serviceCharge", cashoutFee.getServiceCharge());
        BigDecimal bigDecimal = new BigDecimal("100");
        // 处理一下吧
        if(cashOutDTO.getAccountType()==1){ // 分润账户
            BigDecimal rate = cashoutFee.getRate().multiply(bigDecimal).stripTrailingZeros();
            ajaxResult.put("rate", rate);
        }else if(cashOutDTO.getAccountType()==2){//补贴账户
            BigDecimal rate = cashoutFee.getRewardRate().multiply(bigDecimal).stripTrailingZeros();
            ajaxResult.put("rate", rate);
        }else if(cashOutDTO.getAccountType()==3){//服务费账户
            BigDecimal rate = cashoutFee.getRewardRate().multiply(bigDecimal).stripTrailingZeros();
            ajaxResult.put("rate", rate);
        }else if(cashOutDTO.getAccountType()==4){//流量费账户
            BigDecimal rate = cashoutFee.getRewardRate().multiply(bigDecimal).stripTrailingZeros();
            ajaxResult.put("rate", rate);
        }
        return ajaxResult;
    }


    @PostMapping("/toCashOut/anXinFu") // 安心付
    public AjaxResult toCashOutNew(@RequestBody CashOutDTO cashOutDTO){
        AjaxResult ajaxResult = AjaxResult.success("我的钱包-去体现数据获取成功");
        // 获取用户信息
        Long userId = LoginUserUtils.getLoginUserId();
        cashOutDTO.setUserId(userId);
        CashOutVO cashOutVO = new CashOutVO();
        if(cashOutDTO.getAccountType() == 1){// 分润钱包
            cashOutVO =  bizMicroInfoMapper.selectProfit(userId,cashOutDTO.getChannelId());
        }else if(cashOutDTO.getAccountType() == 2){// 机具返现钱包
            cashOutVO =  bizMicroInfoMapper.selectReward(userId,cashOutDTO.getChannelId());
        }else{ // 奖励钱包
            cashOutVO =  bizMicroInfoMapper.selectWithdrawal(userId,cashOutDTO.getChannelId());
        }
        // 银行卡需要最后四位
        String cardNo = cashOutVO.getMerchBankCardno();
        if(!"".equals(cardNo) && cardNo.length()>4 && cashOutDTO.getEncryptionCardno()==1){
            cardNo = cardNo.substring(cardNo.length()-4);
            cashOutVO.setMerchBankCardno(cardNo);
        }
        ajaxResult.put("data", cashOutVO);
        WithdrawalRateTemplate withdrawalRateTemplate = bizWithdrawalRecordsMapper.getDefaultWithdrawalRateTemplate();
        ajaxResult.put("rate", withdrawalRateTemplate.getProfitRate());
        ajaxResult.put("minAmount", withdrawalRateTemplate.getVar3());
        ajaxResult.put("serviceCharge", withdrawalRateTemplate.getProfitFee());
        return ajaxResult;
    }



    @ApiOperation(value = "提现-核对密码")
    @PostMapping("/confirmPassword")
    public AjaxResult confirmPassword(@RequestBody CashOutDTO cashOutDTO) {
        AjaxResult ajaxResult = AjaxResult.success("提现-核对密码数据获取成功");
        // 获取用户信息
        Long userId = LoginUserUtils.getLoginUserId();
        cashOutDTO.setUserId(userId);
        boolean b = microInfoService.confirmPaymentPassword(cashOutDTO);
        ajaxResult.put("data", b);
        return ajaxResult;
    }

    @ApiOperation(value = "预支付提现")
    @PostMapping("/doCashOut")
    public AjaxResult doCashOut(@RequestBody CashOutDTO cashOutDTO)
            throws Exception {
        return AjaxResult.error("请更新APP");
       /* // 获取用户信息
        Long userId = LoginUserUtils.getLoginUserId();
        // 获取用户信息
        cashOutDTO.setUserId(userId);
        // 提现预支付
        // AjaxResult ajaxResult =  walletService.doCashOut(cashOutDTO);
        AjaxResult ajaxResult =  walletService.pay(cashOutDTO);
        return ajaxResult;*/
    }
    // 易支付接口实现
    @ApiOperation(value = "易支付接口预支付提现")
    @PostMapping("/pay")
    public AjaxResult pay(@RequestBody CashOutDTO cashOutDTO)
            throws Exception {
        return AjaxResult.error("请更新APP");
       /* // 获取用户信息
        Long userId = LoginUserUtils.getLoginUserId();
        // 获取用户信息
        cashOutDTO.setUserId(userId);
        // 提现预支付
        AjaxResult ajaxResult =  walletService.pay(cashOutDTO);
        return ajaxResult;*/
    }

    @Override
    public void setEnvironment(Environment environment) {
        appId = config.getAygAppid();
        privateKey = config.getAppRsaPrivatekey();
        client = new OpenApiClient.Builder().appId(appId).privateKey(privateKey).useSandbox().build();
        PosV2AppPaymentController.client = client;
    }
}
